<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController {

    /**
     * @var string the default layout for the controller view. Defaults to 'column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout = 'column1';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu = array();

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();
    public $webOptions; //网站共公设置

    /**
     * 初始化网站
     */

    function init() {
        parent::init();
        $this->webOptions = WebOption::model()->findByPk(1);
    }

    /**
     * 判断角色权限
     * @param string $controller 控制器
     * @param string $action 方法
     * @return string allow, deny
     */
    public function checkAccess($controller, $action) {
        $rid = Yii::app()->request->cookies['rid'];
        if (empty($rid->value) || Yii::app()->user->isGuest) {
            return 'deny';
        } else {
            $model = new SysRoleAcl;
            return $model->checkAccess($controller, $action);
        }
    }

    /**
     * 生成栏目URL
     * @param type $rute
     * @param type $params
     * @param type $category_type
     * @return type
     */
    public function createCategoryUrl($params = array(), $category_type = 'category') {
        switch ($category_type) {
            case 'PRODUCT'://产品综合首页
                return Yii::app()->createAbsoluteUrl('category/index', $params);
                break;
            case 'ARCHIVE'://文章综合首页
            case 'news':
                return Yii::app()->createAbsoluteUrl('news/list', $params);
                break;
            case 'page'://单面
            case 'pictures':
                return Yii::app()->createAbsoluteUrl('news/page', $params);
                break;
            default:
                if (isset($params['master']) && isset($params['alias'])) {
                    return Yii::app()->createAbsoluteUrl('category/product', $params);
                } else {
                    $alias = isset($params['alias']) ? $params['alias'] : $params['master'];
                    return Yii::app()->createAbsoluteUrl('category/index', array('master' => $alias));
                }

                break;
        }
    }

    /**
      +-----------------------------------------------------
     * 说明货币兑换 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function changeCurrency($price) {
        $cModel = $this->getMoneyObj();
        $tag = !empty($cModel) ? $cModel->currency_tag : '$';
        $price = !empty($cModel) ? round($price * $cModel->currency_rate, 2) : $price;
        return array('tag' => $tag, 'price' => $price);
    }

    /**
      +-----------------------------------------------------
     * 说明 获取货币
     * @param 
     * @return object
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getMoneyObj() {
        $cookie = Yii::app()->request->getCookies();
        $pk = isset($cookie['siteCurrency']) ? $cookie['siteCurrency']->value : 'USD';
        return WebCurrency::model()->find("currency_code='" . $pk . "'");
    }

}
